

package com.bei_en.teacher.admin.controller.consultation;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;





import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import com.bei_en.teacher.admin.model.consultation.bean.Consultation;
import com.bei_en.teacher.admin.model.consultation.condition.ConsultationCollectionCondition;
import com.bei_en.teacher.admin.model.consultation.condition.ConsultationCommentCondition;
import com.bei_en.teacher.admin.model.consultation.condition.ConsultationCondition;
import com.bei_en.teacher.admin.model.consultation.condition.ConsultationPraiseCondition;
import com.bei_en.teacher.admin.model.consultation.vo.ConsultationCollectionVo;
import com.bei_en.teacher.admin.model.consultation.vo.ConsultationPraiseVo;
import com.bei_en.teacher.admin.model.consultation.vo.ConsultationVo;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.other.Consts;
import com.bei_en.teacher.admin.service.consultation.ConsultationCollectionService;
import com.bei_en.teacher.admin.service.consultation.ConsultationCommentService;
import com.bei_en.teacher.admin.service.consultation.ConsultationPraiseService;
import com.bei_en.teacher.admin.service.consultation.ConsultationService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.bei_en.common.web.MySessionContext;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-8-3 10:49:28
 */
@Controller
@RequestMapping("/api/Consultation")
public class ConsultationAPPController extends BaseController {
    @Autowired
    ConsultationCollectionService consultationCollectionService;
    @Autowired
    ConsultationService consultationService;
    @Autowired
    ConsultationPraiseService consultationPraiseService;
	private static String bucketName = "jiaoshi";
	private static String img_endpoint = "oss-cn-hangzhou.aliyuncs.com";// 图片服务
	@Autowired
	ConsultationCommentService consultationCommentService;
    /**
     * 咨询列表
     * @param model
     * @param vo
     * @param br
     * @param session
     * @return
     */
    @RequestMapping("/consultation")
	@ResponseBody
    public JsonResult consultation(Model model,ConsultationCondition vo, BindingResult br, HttpSession session ) {

		List<ConsultationVo> list =new ArrayList<ConsultationVo>();
		   list = consultationService.findConsultationPage(vo);
		   JSONArray array=new JSONArray();
		   ConsultationVo consultationVo=null;
		   JSONObject object=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   consultationVo=list.get(i);
			   object.put("title",  consultationVo.getTitle());
			   object.put("Id",  consultationVo.getId());
			   object.put("create_time",  consultationVo.getCreateTime().getTime() 	);
			   object.put("look_num",   	consultationVo.getLookNum());
			   seetingJsonObject(object, "thumbnail", "http://" + bucketName + "."
						+ img_endpoint + "/" +consultationVo.getThumbnail());
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
    }
    /**
     * 咨询详情  
     * @param model
     * @param vo
     * @param br
     * @param session
     * @return
     */
    @RequestMapping("/consultationDetails")
    @ResponseBody
    public JsonResult consultationDetails(Model model,@Valid ConsultationCondition vo, BindingResult br, HttpSession session) {
    	UsersVo user = (UsersVo) session
				.getAttribute(Consts.LOGIN_USER);
    	int user_id=0;
    	if(user!=null){
    		   HttpSession session2 = MySessionContext.getSession(user.getUserId());
   		    if (session2==null||!session.getId().equals(session2.getId())) {
   			    log.info("已被踢掉:"+session.getId()+","+user.getId());
   		    }else{
   		    	   user_id=user.getId();
   		    }
    	}
		
     	if (br!=null&&br.hasErrors()){
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
    				.getDefaultMessage());
       	}
     	
       	ConsultationVo   consultation= 	consultationService.findById(vo.getId());
       	consultation.setLookNum(consultation.getLookNum()+1);
       	consultationService.updateSelective(consultation);
       	
       	consultation.setContent(HtmlUtils.htmlUnescape(consultation.getContent()));
       	System.out.println(consultation.getContent());
       	JSONObject ob=new JSONObject();
       	ob.put("content", ""+"<p align=\"center\"><span style=\"font-size:18px;\"><strong>"
       		    +consultation.getTitle()+"</strong></span></p><p><span style=\"color:#CCCCCC;\">时间:  "
       		    +parseDate(consultation.getCreateTime())+"&emsp;&emsp;浏览量："+consultation.getLookNum()
       		    +"次</span></p><p></p>"+consultation.getContent());
       	ob.put("create_time",  consultation.getCreateTime().getTime() 	);
       	ob.put("look_num",   	consultation.getLookNum());
       	//点赞数
  		ConsultationPraiseCondition condition=new ConsultationPraiseCondition();
  		condition.setConsultatinId(consultation.getId());
   		ob.put("praise_num",consultationPraiseService.searchConsultationPraiseNum(condition)+"");
   		
       	if(user_id>0){
       		ConsultationCollectionCondition collectionCondition=new ConsultationCollectionCondition();
       		collectionCondition.setConsultationId(consultation.getId());
       		collectionCondition.setCollectionPeople(user_id);
       		List<ConsultationCollectionVo>  collectionVos=		consultationCollectionService.findConsultationCollectionPage(collectionCondition);
       		if(!collectionVos.isEmpty()){
       			ob.put("isCollection", "1");
       		}else{
       			ob.put("isCollection", "0");
       		}
       		//是否点赞
       		condition.setPraisePeople(user_id);
       		List<ConsultationPraiseVo>  consultationPraiseVos=consultationPraiseService.findConsultationPraisePage(condition);

       	
       		if(!consultationPraiseVos.isEmpty()){
       			ob.put("isPraise", "1");
       		}else{
       			ob.put("isPraise", "0");
       		}
       		
       	}else{
			ob.put("isPraise", "0");
			ob.put("isCollection", "0");
       	}
    	ob.put("url", "0");
   		ob.put("title", consultation.getTitle());
   		seetingJsonObject(ob, "thumbnail", "http://" + bucketName + "."
				+ img_endpoint + "/" +consultation.getThumbnail());
   		ob.put("create_time",  consultation.getCreateTime().getTime() 	);
   		ConsultationCommentCondition commentCondition=new ConsultationCommentCondition();
   		commentCondition.setConsultationId(consultation.getId());
   		ob.put("comment_num",consultationCommentService.searchConsultationCommentNum(commentCondition));
	return new JsonSuccessResult(session.getId(),  ob);
    }

    private static String parseDate(Date date) {

	long time = date.getTime();
	long timeNow = new Date().getTime();
	long diff = timeNow-time;
	if (diff>0&&diff<24l*3600*1000)
	    return diff/(3600l*1000)+"小时前";
	else if (diff>=24l*3600*1000&&diff<31l*24*3600*1000)
	    return diff/(24l*3600*1000)+"天前";
	else if (diff>=31l*24*3600*1000&&diff<12l*30*24*3600*1000)
	    return diff/(30l*24*3600*1000)+"个月前";
	else
	    return diff/(12l*30*24*3600*1000)+"年前";

    }
    @RequestMapping("/consultationAdd")
    @ResponseBody
    public JsonResult consultationAdd(Model model,ConsultationCondition vo, BindingResult br, HttpSession session) {
       	ConsultationVo consultation=new ConsultationVo();
	consultationService.insert(consultation);
	return new JsonSuccessResult(session.getId(), new Array());
    }

    @RequestMapping("/consultationEdit")
    public JsonResult consultationEdit(Model model,ConsultationCondition vo, BindingResult br, HttpSession session) {
	
    	ConsultationVo consultation=new ConsultationVo();
	
		consultationService.updateSelective(consultation);
	
		return new JsonSuccessResult(session.getId(), 	new Array());
    }

  
    
}
